package com.lanou3g.hw0419.q2;

import java.io.*;
import java.util.Random;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/4/20;
 * Otherwise, I don't know who created it either
 */

public class Main {
    // 目标是1G
    public static final long TARGET = 1024L * 1024 * 1024;

    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                createFile("a",
                        "C:\\Users\\73448\\Desktop\\作业\\Q2");
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                createFile("b",
                        "C:\\Users\\73448\\Desktop\\作业\\Q2");
            }
        }.start();
    }

    private static void createFile(String fileName,
                                   String filePath) {
        File file = new File(filePath, fileName);
        BufferedOutputStream bos = null;

        try {
            bos = new BufferedOutputStream(
                    new FileOutputStream(file));
            Random random = new Random();
            byte[] buf = new byte[1024];
            random.nextBytes(buf);
            long sum = 0;
            while (sum < TARGET) {
                bos.write(buf);
                sum += 1024L;
            }
            bos.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
